package com.hxkj.file.controller;

import com.hxkj.file.entity.po.FileContent;
import com.hxkj.file.service.FileService;
import com.platform.common.domain.exception.CustomMessageException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import static com.hxkj.file.entity.constant.FileTypeConstant.IMG;

/**
 * 文件上传控制器
 * <p>
 * Created by chenwei on 2018/12/11.
 */
@RestController
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/upload")
    public FileContent upload(@RequestParam("type") String type, @RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if(file == null){
            throw new CustomMessageException("请选择需要上传的文件");
        }
        String src = null;
        switch (type) {
            case IMG:
                src = fileService.imgUpload(file);
        }
        FileContent fileContent = FileContent.builder().src(src).build();
        return fileContent;
    }
}
